package co.yixiang.yshop.module.print.service.webprint;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.print.controller.admin.webprint.vo.WebPrintCreateReqVO;
import co.yixiang.yshop.module.print.controller.admin.webprint.vo.WebPrintExportReqVO;
import co.yixiang.yshop.module.print.controller.admin.webprint.vo.WebPrintPageReqVO;
import co.yixiang.yshop.module.print.controller.admin.webprint.vo.WebPrintUpdateReqVO;
import co.yixiang.yshop.module.print.convert.webprint.WebPrintConvert;
import co.yixiang.yshop.module.print.dal.dataobject.webprint.WebPrintDO;
import co.yixiang.yshop.module.print.dal.mysql.webprint.WebPrintMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.print.enums.ErrorCodeConstants.*;

/**
 * 易联云打印机 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class WebPrintServiceImpl implements WebPrintService {

    @Resource
    private WebPrintMapper webPrintMapper;

    @Override
    public Long createWebPrint(WebPrintCreateReqVO createReqVO) {
        // 插入
        WebPrintDO webPrint = WebPrintConvert.INSTANCE.convert(createReqVO);
        webPrintMapper.insert(webPrint);
        // 返回
        return webPrint.getId();
    }

    @Override
    public void updateWebPrint(WebPrintUpdateReqVO updateReqVO) {
        // 校验存在
        validateWebPrintExists(updateReqVO.getId());
        // 更新
        WebPrintDO updateObj = WebPrintConvert.INSTANCE.convert(updateReqVO);
        webPrintMapper.updateById(updateObj);
    }

    @Override
    public void deleteWebPrint(Long id) {
        // 校验存在
        validateWebPrintExists(id);
        // 删除
        webPrintMapper.deleteById(id);
    }

    private void validateWebPrintExists(Long id) {
        if (webPrintMapper.selectById(id) == null) {
            throw exception(WEB_PRINT_NOT_EXISTS);
        }
    }

    @Override
    public WebPrintDO getWebPrint(Long id) {
        return webPrintMapper.selectById(id);
    }

    @Override
    public List<WebPrintDO> getWebPrintList(Collection<Long> ids) {
        return webPrintMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WebPrintDO> getWebPrintPage(WebPrintPageReqVO pageReqVO) {
        return webPrintMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WebPrintDO> getWebPrintList(WebPrintExportReqVO exportReqVO) {
        return webPrintMapper.selectList(exportReqVO);
    }

}
